<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="formModel">
        <el-form-item class="no-warn-item" label="产品公告号:" prop="certificate">
          <el-input
            v-model="formModel.certificate"
            class="filter-item "
            clearable
          />
        </el-form-item>
        <el-form-item class="no-warn-item" label="整车物料号:" prop="materialNo">
          <el-input
            v-model="formModel.materialNo"
            class="filter-item "
            clearable
          />
        </el-form-item>
        <el-form-item class="no-warn-item" label="状态:" prop="status">
          <hey-select v-model="formModel.status" clcd="is_effective_cd" />
        </el-form-item>
        <el-row class="submit-area">
          <el-col :span="24">
            <el-button
              v-if="hasPermission('certificateMaterialNo_select')"
              type="primary"
              plain
              @click="queryList(false)"
            >查询
            </el-button>
            <el-button v-if="hasPermission('certificateMaterialNo_add')" type="primary" plain @click="handleAdd">新增
            </el-button>
            <el-button type="primary" plain @click="handleExport">导出
            </el-button>
            <el-button type="primary" plain @click="handleImport">导入
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <hey-table ref="table" :table-data="tableData" :columns="columns">
      <template slot="tools">
        <el-table-column
          label="操作"
          width="120"
          fixed="right"
          header-align="center"
          align="center"
        >
          <template slot-scope="{row}">
            <span
              v-if="hasPermission('certificateMaterialNo_edit')"
              class="hey_tools_btn"
              @click="handleEdit(row)"
            >编辑</span>
            <span
              v-if="hasPermission('certificateMaterialNo_del')"
              class="hey_tools_btn"
              @click="handleDelete(row)"
            >删除</span>
          </template>
        </el-table-column>
      </template>
    </hey-table>
    <el-dialog
      v-if="dialogVisible"
      v-dialogDrag
      :close-on-click-modal="false"
      title="产品公告号与物料关系信息"
      :visible.sync="dialogVisible"
      width="90%"
    >
      <CertificateMaterialNo-mnt :page-type="operateType" :edit-data="editData" @mntSuc="queryList" />
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        action=""
        :file-list="fileList"
        :on-change="handleChange"
        :on-remove="handleRemove"
        :http-request="uploadSectionFile"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div slot="tip" class="el-upload__tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:#ff0000">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUpload">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

export default {
  name: 'CertificateMaterialNoList',
  components: {
    CertificateMaterialNoMnt: () => import('./certificateMaterialNo-mnt')
  },
  props: {},
  data() {
    return {
      dialogVisible: false,
      operateType: '',
      editData: {},
      tableData: [],
      fileList: [],
      upload: {
        // 是否显示弹出层
        open: false
      },
      // 查询条件
      formModel: {
        certificate: '',
        materialNo: '',
        status: ''
      },
      columns: [
        { title: '整车物料号', key: 'materialNo' },
        { title: '车辆型号', key: 'clxh' },
        { title: '一致性证书编号', key: 'consistencyCertificateNo' },
        { title: '环保证书编号', key: 'environmentCertificateNo' },
        { title: '合格证书编号', key: 'qualifiedCertificateNo' },
        { title: '三包证书编号', key: 'threeGuaranteeNo' },
        { title: '能源消耗标识备案号', key: 'bah' },
        { title: '一致性证书模板', key: 'consistencyCertificateModelFileName', clcd: 'print_model_cd' },
        { title: '环保证书模板', key: 'environmentCertificateModelFileName', clcd: 'print_model_cd' },
        { title: '合格证书模板', key: 'qualifiedCertificateModelFileName', clcd: 'print_model_cd' },
        { title: '三包证书模板', key: 'threeGuaranteeModelFileName', clcd: 'print_model_cd' },
        { title: '能源消耗标识模板', key: 'fuelModelFileName', clcd: 'print_model_cd' },
        { title: '配置序列号', key: 'pzxlh' },
        { title: '轮胎规则', key: 'ltgg' },
        { title: '车辆颜色', key: 'clys' },
        { title: '驱动形式', key: 'qdxs' },
        { title: 'VIN码前八位', key: 'vinQbw' },
        { title: '合格证备注', key: 'remarks' },

        { title: '状态', key: 'status', clcd: 'is_effective_cd' },
        { title: '创建时间', key: 'createTime' },
        { title: '创建人', key: 'createUsername' },
        { title: '最后修改时间', key: 'lastUpdateTime' },
        { title: '最后修改人', key: 'lastUpdateUsername' }
      ]
    }
  },
  computed: {},
  methods: {
    queryList() {
      this.$refs.table.query('/certificateMaterialNo/pageCertificateMaterialNo', this.formModel, '', false)
    },
    handleAdd() {
      this.operateType = 'add'
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.operateType = 'edit'
      this.editData = row || {}
    },
    handleDelete(row) {
      this.$sure('确认删除？', _ => {
        this.post('/certificateMaterialNo/deleteCertificateMaterialNo', {
          certificateMaterialNoId: row.certificateMaterialNoId
        }).then(_ => {
          this.$info('删除成功', this.queryList)
        })
      })
    },
    // 导入
    handleImport() {
      this.upload.open = true
    },
    // 导出
    handleExport() {
      const url = '/certificateMaterialNo/exportCertificateMaterialNo'
      this.postUseBlob(url, this.formModel).then(res => {
        const blob = new Blob([res], {
          type: 'application/vnd.ms-excel'
        })
        const fileName = '证书与物料号关系维护.xls'
        if ('download' in document.createElement('a')) {
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else {
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    // 下载模板
    importTemplate() {
      const url = '/certificateMaterialNo/importTemplateCertificateMaterialNo'
      const param = {}
      this.postUseBlob(url, param).then(res => {
        const blob = new Blob([res], {
          type: 'application/vnd.ms-excel'
        })
        const fileName = '证书与物料号关系导入模板.xls'
        if ('download' in document.createElement('a')) {
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else {
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    handleChange(file, fileList) {
      this.fileList = fileList
    },
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    // 校验文件数量
    submitUpload() {
      if (this.fileList.length === 0) {
        this.$message.warning('请选取一个文件!')
        return false
      }
      if (this.fileList.length > 1) {
        this.$message.warning('单次只能上传一个文件!')
        return false
      }
      this.$refs.upload.submit()
    },
    // 上传excel文件
    uploadSectionFile(param) {
      var fileObj = param.file
      const url = '/certificateMaterialNo/importCertificateMaterialNo'
      // FormData 对象
      var form = new FormData()
      form.append('file', fileObj)
      const tip = '文件上传成功!'
      this.post(url, form).then(res => {
        this.$info(tip, () => {
          this.$emit('mntSuc')
          this.upload.open = false
          this.fileList = []
        })
      })
    }
  }
}
</script>
